import { category1ListApi, category2ListApi, category3ListApi } from "@/api/product/category";
import {defineStore}  from "pinia"
import type { CategoryStore } from "./interface"

const categoryStore=defineStore("category",{
  state:():CategoryStore=>{
    return {
      category1Id:undefined, //选择的一级id
      category2Id:undefined, //选择的一级id
      category3Id:undefined,//选择的一级id
      category1List:[], //一级列表
      category2List:[], //一级列表
      category3List:[], //一级列表
    }
  },
  actions:{
    // 请求获取一级分类列表并更新category1List
    async getCategory1List(){
      this.category1List=await category1ListApi()
    },
    // 请求获取二级分类列表并更新category2List
    async getCategory2List(category1Id:number){
       // 将传入的一级id保存到state中
       this.category1Id = category1Id
      this.category2List=await category2ListApi(category1Id)
       // 清除二三级分类id, 三级列表
       this.category2Id = undefined
       this.category3Id = undefined
       this.category3List = []
       
    },
    // 请求获取三级分类列表并更新category3List
    async getCategory3List(category2Id:number){
       // 将传入的一级id保存到state中
      this.category2Id = category2Id
      this.category3List=await category3ListApi(category2Id)
      // 清除三级ID
      this.category3Id = undefined
    },

    // 清空
    removeCategory(){
      this.category1Id=undefined; 
      this.category2Id=undefined; 
      this.category3Id=undefined;
      this.category1List=[]; 
      this.category2List=[]; 
      this.category3List=[]; 
    }
  },
  getters:{}
})

export default categoryStore